<script setup>
import { ref, nextTick, onMounted } from "vue";
// 引入 echarts 核心模块，核心模块提供了 echarts 使用必须要的接口。
import * as echarts from "echarts/core";
// 引入折线图图表，图表后缀都为 Chart
import { LineChart } from "echarts/charts";
// 引入提示框，标题，直角坐标系，数据集，内置数据转换器组件，组件后缀都为 Component
import {
  TitleComponent,
  ToolboxComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
} from "echarts/components";
// 标签自动布局、全局过渡动画等特性
import { UniversalTransition } from "echarts/features";
// 引入 Canvas 渲染器，注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from "echarts/renderers";

// 注册必须的组件
echarts.use([
  TitleComponent,
  ToolboxComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  LineChart,
  UniversalTransition,
  CanvasRenderer,
]);

// 接收数据
const props = defineProps({
  row: {
    type: Number,
    default: 0,
  },
  col: {
    type: Number,
    default: 0,
  },
  argList: {
    type: Array,
    default: function () {
      return [];
    },
  },
  tableHead: {
    type: Array,
    default: function () {
      return [];
    },
  },
});

// 修改数据格式
const legend = ref([]);
const xAxisData = ref([]); // 元素：字符串
const yAxisData = ref([]); // 元素：对象，有name和data[]两个属性
const initData = () => {
  if (props.row === 0 || props.col === 0) {
    return;
  }
  // props.row + 1：算上x0
  for (let i = 0; i < props.row + 1; i++) {
    const oneLine = {};
    let originData = [];
    oneLine.data = [];
    oneLine.type = "line";
    oneLine.name = props.argList[i][String(0)];
    legend.value.push(props.argList[i][String(0)]);
    // 原始数据
    for (let j = 1; j <= props.col; j++) {
      originData.push(Number(props.argList[i][String(j)]));
    }
    // 取最小值
    let min = originData[0];
    for (let j = 0; j < originData.length; j++) {
      if (originData[i] < min) {
        min = originData[i];
      }
    }
    // 标准化
    for (let j = 0; j < props.col / 2; j++) {
      let t = (originData[2 * j] / min + originData[2 * j + 1] / min) / 2;
      oneLine.data.push(t);
    }
    yAxisData.value.push(oneLine);
  }
  for (let i = 1; i <= props.col / 2; i++) {
    xAxisData.value.push(String(props.tableHead[i]));
  }
};
initData();

// 初始化图表
const optionForm = {
  title: {
    text: "数据变化趋势",
  },
  tooltip: {
    trigger: "axis",
  },
  legend: {
    data: [],
  },
  grid: {
    left: "3%",
    right: "4%",
    bottom: "3%",
    containLabel: true,
  },
  toolbox: {
    feature: {
      saveAsImage: {},
    },
  },
  xAxis: {
    type: "category",
    boundaryGap: false,
    data: [],
  },
  yAxis: {
    type: "value",
  },
  series: [],
};
const option = ref({ ...optionForm });
const initChart = () => {
  // 接下来的使用就跟之前一样，初始化图表，设置配置项
  const myChart = echarts.init(document.getElementById("main"));
  // 加数据
  option.value.legend.data = legend.value;
  option.value.xAxis.data = xAxisData.value;
  option.value.series = yAxisData.value;
  myChart.setOption(option.value);
};

onMounted(() => {
  nextTick(() => {
    initChart();
  });
});
</script>

<template>
  <div id="main"></div>
</template>

<style scoped>
#main {
  height: 300px;
  width: 800px;
}
</style>